package vista.modelo;

import modelo.Edge;
import modelo.Node;

import android.graphics.Path;

import vista.Geometry;

public class VEdge extends Edge {

	public boolean moved;

	public Path path;
	public Path arrow;

	public VEdge(String wordsSeparetedWithComa, VNode tail, VNode head) {
		super(wordsSeparetedWithComa, tail, head);
	}

	public Path getPath(int xWindow, int yWindow) {
		if(path == null || moved)
			generatePath(xWindow, yWindow);
		moved = false;
		return path;
	}

	private void generatePath(int xWindow, int yWindow) {

		int halfRadius = ((VNode)((VNode)tail)).radius/2;
		int startX = ((VNode)((VNode)tail)).x - halfRadius - xWindow;
		int startY = ((VNode)((VNode)tail)).y - halfRadius - yWindow;
		int endX = ((VNode)((VNode)head)).x - halfRadius - xWindow;
		int endY = ((VNode)((VNode)head)).y - halfRadius - yWindow;

		if(((VNode)head) != ((VNode)tail)) {

			x = (startX + endX)/2;
			y = (startY + endY)/2;
			if(((VNode)((VNode)tail)).x < ((VNode)((VNode)head)).x) {
				y -= 30;
			} else {
				y += 30;
			}
			if(((VNode)((VNode)tail)).y < ((VNode)head).y) {
				x += 30;
			} else {
				x -= 30;
			}

			path = new Path();
			path.moveTo(startX, startY);
			path.lineTo(x, y);
			path.lineTo(endX, endY);

		} else {

			x = (int) (startX - ((VNode)((VNode)head)).radius*2.5);
			y = startY;

			path = new Path();
			path.moveTo(startX, startY);
			path.lineTo(x + 20, startY + ((VNode)((VNode)head)).radius + 10);
			path.lineTo(x + 10, startY + ((VNode)head).radius + 10);
			path.lineTo(x - 10, startY + ((VNode)head).radius - 10);
			path.lineTo(x, y);
			path.lineTo(endX, endY);
		}

		// Arrow
		double pointingAngle = Geometry.angleBetweenTwoPoints(x, y, endX, endY);
		double xArrow = endX + ((VNode)head).radius*Math.cos(pointingAngle);
		double yArrow = endY + ((VNode)head).radius*Math.sin(pointingAngle);
		pointingAngle -= Math.PI/6;
		double x2Arrow = xArrow + 20*Math.cos(pointingAngle);
		double y2Arrow = yArrow + 20*Math.sin(pointingAngle);
		pointingAngle += (Math.PI*4)/6;
		double x3Arrow = x2Arrow + 18*Math.cos(pointingAngle);
		double y3Arrow = y2Arrow + 18*Math.sin(pointingAngle);
		arrow = new Path();
		arrow.moveTo((int)xArrow, (int)yArrow);
		arrow.lineTo((int)x2Arrow, (int)y2Arrow);
		arrow.lineTo((int)x3Arrow, (int)y3Arrow);
	}
	
	public Edge clone(Node tail, Node head) {
		return new VEdge(inputWordsString+"", (VNode)tail, (VNode)head);
	}

}
